﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace LD
{
    [Serializable]
    public class ReDrawUI
    {
        [Serializable]
        public struct ControlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
            public float FontSize;
        }

        public static bool programShown = false;
        public Dictionary<string, ControlRect> oldCtrl = new Dictionary<string, ControlRect>();
        public Dictionary<string, ControlRect> curCtrl = new Dictionary<string, ControlRect>();

        private bool bFirstLoad = false;
        private int ctrlNo;

        public void ControlInitializeSize(Control mForm)
        {
            ControlRect cR;
            cR.Left = mForm.Left;
            cR.Top = mForm.Top;
            cR.Width = mForm.ClientSize.Width;
            cR.Height = mForm.ClientSize.Height;
            cR.FontSize = mForm.Font.Size;

            InsertDictionary(mForm.Name, cR);
            AddControl(mForm);
            bFirstLoad = true;
        }

        private void AddControl(Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                ControlRect objCtrl;
                objCtrl.Left = c.Left;
                objCtrl.Top = c.Top;
                objCtrl.Width = c.Width;
                objCtrl.Height = c.Height;
                objCtrl.FontSize = c.Font.Size;

                InsertDictionary(c.Name, objCtrl);

                if (c.Controls.Count >= 1)
                {
                    if (c.GetType() == typeof(SplitContainer))
                    {
                        int Index = 1;
                        foreach (Control mdiCtrl in c.Controls)
                        {
                            mdiCtrl.Name = c.Name + "Panel" + Index.ToString();
                            Index++;
                        }
                    }
                    AddControl(c);
                }
            }
        }

        private void InsertDictionary(String name, ControlRect cr)   //添加控件名和位置，如果名称重复则更新
        {
            Dictionary<String, ControlRect> temp = new Dictionary<String, ControlRect>();
            bool flag = false;
            foreach (var pair in oldCtrl)
            {
                if (pair.Key.ToString() == name)
                {
                    temp.Add(name, cr);
                    flag = true;
                }
            }
            if (flag == false)
            {
                oldCtrl.Add(name, cr);
                curCtrl.Add(name, cr);
            }
            foreach (var value in temp)
            {
                oldCtrl.Remove(value.Key.ToString());
                oldCtrl.Add(value.Key, value.Value);
                curCtrl.Remove(value.Key.ToString());
                curCtrl.Add(value.Key, value.Value);
            }
            temp.Clear();
        }

        private void AutoScaleControl(Control ctl, float wScale, float hScale)
        {
            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
            float ctrFontSize0;
            foreach (Control c in ctl.Controls)
            {
                ctrLeft0 = oldCtrl[c.Name].Left;
                ctrTop0 = oldCtrl[c.Name].Top;
                ctrWidth0 = oldCtrl[c.Name].Width;
                ctrHeight0 = oldCtrl[c.Name].Height;
                ctrFontSize0 = oldCtrl[c.Name].FontSize;

                int newLeft, newTop, newWidth, newHeight;
                newLeft = (int)((ctrLeft0) * wScale);
                newTop = (int)((ctrTop0) * hScale);
                newWidth = (int)(ctrWidth0 * wScale);
                newHeight = (int)(ctrHeight0 * hScale);
                Single currentSize = ctrFontSize0 * Math.Min(wScale, hScale);

                if (newLeft == curCtrl[c.Name].Left && newTop == curCtrl[c.Name].Top && newWidth == curCtrl[c.Name].Width && newHeight == curCtrl[c.Name].Height)
                    continue;
                else
                {
                    c.Left = newLeft;
                    c.Top = newTop;
                    c.Width = newWidth;
                    c.Height = newHeight;
                    if (currentSize != 0)
                    {
                        try
                        {
                            c.Font = new Font(c.Font.Name, currentSize, c.Font.Style, c.Font.Unit);
                        }
                        catch
                        {
                            break;
                        }
                    }

                    ControlRect newCtrl = new ControlRect();
                    newCtrl.Left = c.Left;
                    newCtrl.Top = c.Top;
                    newCtrl.Width = c.Width;
                    newCtrl.Height = c.Height;
                    newCtrl.FontSize = currentSize;
                    curCtrl.Remove(c.Name);
                    curCtrl.Add(c.Name, newCtrl);
                }

                ctrlNo++;
                if (c.Controls.Count > 0)
                    AutoScaleControl(c, wScale, hScale);

            }
        }

        public void ControlAutoSize(Control mForm)
        {
            if (!bFirstLoad)
                return;

            float wScale, hScale;
            if (mForm.ClientSize.Width == curCtrl[mForm.Name].Width && mForm.ClientSize.Height == curCtrl[mForm.Name].Height)
            {

                return;
            }
            else
            {
                wScale = (float)mForm.ClientSize.Width / oldCtrl[mForm.Name].Width;
                hScale = (float)mForm.ClientSize.Height / oldCtrl[mForm.Name].Height;

                ControlRect newCtrl = new ControlRect();
                newCtrl.Left = mForm.Left;
                newCtrl.Top = mForm.Top;
                newCtrl.Width = mForm.Width;
                newCtrl.Height = mForm.Height;
                newCtrl.FontSize = mForm.Font.Size;
                curCtrl.Remove(mForm.Name);
                curCtrl.Add(mForm.Name, newCtrl);
            }

            if (wScale == 0 || hScale == 0)
                return;
            ctrlNo = 1;
            AutoScaleControl(mForm, wScale, hScale);
        }
    }
}
